package com.buybuyyi.stylistcrm.order.helper;

import com.alibaba.fastjson.JSONArray;
import com.buybuyyi.stylistcrm.utils.StringUtil;
import stylistcrm.core.services.order.dto.*;
import stylistcrm.core.services.order.enums.EnumOrderType;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheny on 2016/9/19.
 */
public class OrderGoodsHelper {

    public static void setGoodsNameAndCount(List<OrderGoodsDto> orderGoodsDtoList,TransferOrderDto transferOrder){
        if(null!=orderGoodsDtoList&&0!=orderGoodsDtoList.size()){
            StringBuilder goodsNames=new StringBuilder();
            Integer totalCount=0;
            for(int i=0;i<orderGoodsDtoList.size();i++){
                OrderGoodsDto goodsDto=orderGoodsDtoList.get(i);
                if(StringUtil.isNotBlank(goodsDto.getGoodsName())){
                    goodsNames.append(goodsDto.getGoodsName());
                    if(i!=orderGoodsDtoList.size()-1){
                        goodsNames.append("、");
                    }
                }
                if(goodsDto.getCount()!=null){
                    totalCount+=goodsDto.getCount();
                }
                if(transferOrder instanceof UserOrderDto){
                    goodsDto.setOrderType(EnumOrderType.USER_ORDER.getCode());
                }
            }
            transferOrder.setCount(totalCount);
            transferOrder.setGoodsNames(goodsNames.toString());
        }
    }

    /**
     * 设置默认排序字段
     * @param queryCondition
     * @param orderType
     */
    public static void setDefaultOrderBy(QueryOrderConditionDto queryCondition, EnumOrderType orderType){
        if(null!=queryCondition){
            if(queryCondition.getOrderBy()==null){
                List<OrderByDto> orderByList=new ArrayList<>();
                OrderByDto orderBy=new OrderByDto();
                if(orderType==EnumOrderType.INVOICE_ORDER){
                    orderBy.setOrderName("i.create_time");
                }else{
                    orderBy.setOrderName("o.order_time");
                }
                orderBy.setOrderType(OrderByDto.EnumOrderByType.DESC);
                orderByList.add(orderBy);
                queryCondition.setOrderBy(orderByList);
            }
        }
    }

    /**
     * 将字符串商品转换为集合
     * @param orderDto
     */
    public static void setGoodsList(TransferOrderDto orderDto){
        if(null == orderDto){
            return;
        }
        String goodsList=orderDto.getGoodsList();
        if(StringUtil.isBlank(goodsList)){
            return;
        }
        List<OrderGoodsDto> orderGoodsDtoList=JSONArray.parseArray(goodsList,OrderGoodsDto.class);
        if(null!=orderGoodsDtoList){
            orderDto.setGoods(orderGoodsDtoList);
        }
    }
}
